package sites.manager.admin.account;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import sites.manager.common.kit.Ret;
import sites.manager.common.model.Account;

/**
 * 用户管理
 */
public class AccountController extends Controller {

    @Inject
    AccountService srv;

    /**
     * 进入用户管理界面
     */
    public void enterAccountMng(){
        render("pageAccountMng.html");
    }

    /**
     * 查询用户信息
     */
    public void getAccounts(){
        String userName = get("userName", "");
        String nickName = get("nickName", "");
        int page = getInt("page", 1);
        int limit = getInt("limit", 10);

        Ret ret = srv.getAccounts(userName, nickName, page, limit);
        renderJson(ret);
    }

    /**
     * 进入用户新增界面
     */
    public void enterAccountAdd(){
        render("respAccountAdd.html");
    }

    /**
     * 保存用户信息
     */
    @Before(AccountValidator.class)
    public void save(){
        Account account = getModel(Account.class, "");
        Ret ret = srv.save(account);
        renderJson(ret);
    }

    /**
     * 更新用户状态
     */
    public void updateStruts(){
        int id = getInt("id");
        boolean flag = getBoolean("status");
        Ret ret = srv.updateStruts(id, flag);

        renderJson(ret);
    }

    /**
     * 解锁
     */
    public void unlock(){
        int id = getInt("id");
        Ret ret = srv.unlock(id);

        renderJson(ret);
    }

    /**
     * 进入编辑界面
     */
    public void enterAccountEdit(){
        int id = getInt(0);
        Account account = srv.getById(id);
        setAttr("account", account);
        render("respAccountEdit.html");
    }

    /**
     * 更新用户信息
     */
    @Before(AccountValidator.class)
    public void update(){
        Account account = getModel(Account.class, "");
        Ret ret = srv.update(account);
        renderJson(ret);
    }

    /**
     * 批量禁用
     */
    public void batchRemove(){
        String ids = get(0);
        Ret ret = srv.batchRemove(ids);
        renderJson(ret);
    }

}
